package cn.ts.mybatis.generator.plugins.generator;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static org.mybatis.generator.internal.util.StringUtility.isTrue;

/**
 * 添加注释
 *
 * @author Created by yl on 2017/05/28.
 */
public class MyCommentGenerator implements CommentGenerator {
    private Properties properties;
    /**
     * The suppress date.
     */
    private boolean suppressDate;
    /**
     * The suppress all comments.
     */
    private boolean suppressAllComments;
    /**
     * The addition of table remark's comments. If suppressAllComments is true, this option is
     * ignored
     */
    private boolean addRemarkComments;

    public MyCommentGenerator() {
        super();
        properties = new Properties();
        suppressDate = false;
        suppressAllComments = false;
        addRemarkComments = false;
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
    }

    @Override
    public void addComment(XmlElement xmlElement) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // xmlElement.addElement(new TextElement("<!--")); //$NON-NLS-1$
        //
        // StringBuilder sb = new StringBuilder();
        // sb.append(" WARNING - "); //$NON-NLS-1$
        // sb.append(MergeConstants.NEW_ELEMENT_TAG);
        // xmlElement.addElement(new TextElement(sb.toString()));
        // xmlElement.addElement(new TextElement(
        // " This element is automatically generated by MyBatis Generator, do not modify."));
        // //$NON-NLS-1$
        // xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$
    }

    @Override
    public void addRootComment(XmlElement rootElement) {
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
        suppressDate =
                isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
        suppressAllComments = isTrue(
                properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
        addRemarkComments = isTrue(
                properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
        // String dateFormatString =
        // properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
        // if (StringUtility.stringHasValue(dateFormatString)) {
        // dateFormat = new SimpleDateFormat(dateFormatString);
        // }
    }

    /**
     * pojo类注释
     */
    @Override
    public void addModelClassComment(TopLevelClass clazz, IntrospectedTable table) {
        if (suppressAllComments || !addRemarkComments) {
            return;
        }
        clazz.addJavaDocLine("/**"); //$NON-NLS-1$
        String remarks = table.getRemarks();
        if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
            for (String remarkLine : remarkLines) {
                clazz.addJavaDocLine(" *   " + remarkLine.trim()); //$NON-NLS-1$
            }
        }
        // clazz.addJavaDocLine(
        //         " * <p>该类对应于数据库表 <strong>" + table.getFullyQualifiedTable() + "</strong>/p>");
        List<IntrospectedColumn> columns = table.getBaseColumns();
        if (columns != null && !columns.isEmpty()) {
            clazz.addJavaDocLine(" * ");
            clazz.addJavaDocLine(" * <pre>");
            clazz.addJavaDocLine(" * <table>");
            clazz.addJavaDocLine(
                    " *  <tr><td>属性名</td><td>数据类型</td><td>表字段名</td><td>表字段描述</td><td>默认值</td></tr>");
            for (IntrospectedColumn column : columns) {
                String jp = column.getJavaProperty();
                String sn = column.getFullyQualifiedJavaType().getShortName();
                String acn = column.getActualColumnName();
                String remark = column.getRemarks();
                remark = remark == null || remark.equalsIgnoreCase("null") ? "" : remark;
                String dv = column.getDefaultValue();
                dv = dv == null || dv.equalsIgnoreCase("null") ? "" : dv;
                clazz.addJavaDocLine(" *  <tr><td>" + jp + "</td><td>" + sn + "</td><td>" + acn
                        + "</td><td>" + remark.trim() + "</td><td>" + dv + "</td></tr>");
            }
            clazz.addJavaDocLine(" * </table>");
            clazz.addJavaDocLine(" * </pre>");
            clazz.addJavaDocLine(" * ");
            clazz.addJavaDocLine(" * @author Created by Mybatis Generator on "
                    + new SimpleDateFormat("yyyy/MM/dd.").format(new Date()));
        }
        clazz.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    /**
     * pojo类属性注释
     */
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        // if (suppressAllComments || "serialVersionUID".equals(field.getName())) {
        // return;
        // }
        //
        // field.addJavaDocLine("/**"); //$NON-NLS-1$
        // String remarks = introspectedColumn.getRemarks();
        // if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
        // field.addJavaDocLine(" * Database Column Remarks:");
        // String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
        // for (String remarkLine : remarkLines) {
        // field.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
        // }
        // }
        //
        // field.addJavaDocLine(" *"); //$NON-NLS-1$
        // field.addJavaDocLine(" * This field was generated by MyBatis Generator.vvvv");
        // //$NON-NLS-1$
        //
        // StringBuilder sb = new StringBuilder();
        // sb.append(" * This field corresponds to the database column "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // sb.append('.');
        // sb.append(introspectedColumn.getActualColumnName());
        // field.addJavaDocLine(sb.toString());
        // field.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
        // innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator.");
        // //$NON-NLS-1$
        //
        // sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // innerEnum.addJavaDocLine(sb.toString());
        // innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
        // innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
        // // $NON-NLS-1$
        //
        // sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // innerClass.addJavaDocLine(sb.toString());
        // innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    /**
     * Example类属性注释
     */
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // field.addJavaDocLine("/**"); //$NON-NLS-1$
        // field.addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$
        //
        // sb.append(" * This field corresponds to the database table "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // field.addJavaDocLine(sb.toString());
        // field.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    /**
     * 生成构造函数等的注释
     */
    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // method.addJavaDocLine("/**"); //$NON-NLS-1$
        // method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
        // //$NON-NLS-1$
        //
        // sb.append(" * This method corresponds to the database table "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // method.addJavaDocLine(sb.toString());
        // method.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    /**
     * Getter方法注释
     */
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // method.addJavaDocLine("/**"); //$NON-NLS-1$
        // method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
        // //$NON-NLS-1$
        //
        // sb.append(" * This method returns the value of the database column "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // sb.append('.');
        // sb.append(introspectedColumn.getActualColumnName());
        // method.addJavaDocLine(sb.toString());
        //
        // method.addJavaDocLine(" *"); //$NON-NLS-1$
        //
        // sb.setLength(0);
        // sb.append(" * @return the value of "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // sb.append('.');
        // sb.append(introspectedColumn.getActualColumnName());
        // method.addJavaDocLine(sb.toString());
        // method.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    /**
     * Setter方法注释
     */
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // method.addJavaDocLine("/**"); //$NON-NLS-1$
        // method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
        // //$NON-NLS-1$
        //
        // sb.append(" * This method sets the value of the database column "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // sb.append('.');
        // sb.append(introspectedColumn.getActualColumnName());
        // method.addJavaDocLine(sb.toString());
        //
        // method.addJavaDocLine(" *"); //$NON-NLS-1$
        //
        // Parameter parm = method.getParameters().get(0);
        // sb.setLength(0);
        // sb.append(" * @param "); //$NON-NLS-1$
        // sb.append(parm.getName());
        // sb.append(" the value for "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // sb.append('.');
        // sb.append(introspectedColumn.getActualColumnName());
        // method.addJavaDocLine(sb.toString());
        // method.addJavaDocLine(" */"); //$NON-NLS-1$
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable,
                                boolean markAsDoNotDelete) {
        // if (suppressAllComments) {
        // return;
        // }
        //
        // StringBuilder sb = new StringBuilder();
        //
        // innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
        // innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
        // //$NON-NLS-1$
        //
        // sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
        // sb.append(introspectedTable.getFullyQualifiedTable());
        // innerClass.addJavaDocLine(sb.toString());
        // innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
    }
}
